"""
Case Type   : GUC
Case Name   : 修改autovacuum_freeze_max_age为其他数据类型，观察其预期结果
Create At   : 2021/1/18
Owner       : opentestcase018
Description :
    1、查询autovacuum_freeze_max_age默认值；
    show autovacuum_freeze_max_age；
    2、修改autovacuum_freeze_max_age为'abc'，观察预期结果；
    gs_guc set -D {cluster/dn1}  -c"autovacuum_freeze_max_age='abc'"
Expect      :
    1、显示默认值4000000000；
    2、修改autovacuum_freeze_max_age失败，预期结果正常；
History     :
"""

import unittest

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger

LOGGER = Logger()
COMMONSH = CommonSH('PrimaryDbUser')


class GucTest(unittest.TestCase):
    def setUp(self):
        LOGGER.info('==Guc_Autovacuum_Case0024开始执行==')
        self.constant = Constant()

        status = COMMONSH.get_db_cluster_status()
        self.assertTrue("Degraded" in status or "Normal" in status)

    def test_guc(self):
        LOGGER.info("==查询autovacuum_freeze_max_age"
                    "期望默认值4000000000=")
        sql_cmd = COMMONSH.execut_db_sql(
            '''show autovacuum_freeze_max_age;''')
        LOGGER.info(sql_cmd)
        self.assertEqual("4000000000", sql_cmd.split("\n")[-2].strip())

        LOGGER.info("==修改autovacuum_freeze_max_age为'abc'"
                    "期望合理报错==")
        LOGGER.info("==期望：修改失败，show参数为默认值==")
        result = COMMONSH.execute_gsguc(
            'set', self.constant.GSGUC_SUCCESS_MSG,
            "autovacuum_freeze_max_age='abc'")
        self.assertFalse(result)
        sql_cmd = COMMONSH.execut_db_sql(
            '''show autovacuum_freeze_max_age;''')
        LOGGER.info(sql_cmd)
        self.assertEqual("4000000000", sql_cmd.split("\n")[-2].strip())

    def tearDown(self):
        LOGGER.info("恢复默认值")
        sql_cmd = COMMONSH.execut_db_sql(
            '''show autovacuum_freeze_max_age;''')
        if "4000000000" != sql_cmd.split("\n")[-2].strip():
            COMMONSH.execute_gsguc('set', self.constant.GSGUC_SUCCESS_MSG,
                                  "autovacuum_freeze_max_age = '4000000000'")
            COMMONSH.restart_db_cluster()
        status = COMMONSH.get_db_cluster_status()
        result = COMMONSH.execute_gsguc(
            'check', '4000000000', 'autovacuum_freeze_max_age')
        self.assertTrue("Degraded" in status or "Normal" in status)
        self.assertTrue(result)
        LOGGER.info("==Guc_Autovacuum_Case0024执行结束==")
